home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QRZ! Ham Radio 8
/
QRZ Ham Radio Callsign Database - Volume 8.iso
/
pc
/
files
/
t_sys5
/
unixkit.tgz
/
unixkit.tar
/
unixkit
/
common
/
bsd_io.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-28
|
11KB
|
515 lines
/* OS- and machine-dependent stuff for 4.[23] BSD UNIX */
/*
FILE: UNIX.io.c
Routines:
asy_init()
asy_stop()
asy_speed()
asy_output()
get_asy()
doasystat()
asy_ioctl()
asy_send()
asy_tx()
stxrdy()
asy_autobaud()
get_rlsd_asy()
asytimer()
dir()
Written or converted by Mikel Matthews, N9DVG, and others
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/dir.h>
#include <sys/time.h>
#include "global.h"
#include "mbuf.h"
#include "internet.h"
#include "iface.h"
#include "proc.h"
#include "asy.h"
#include "cmdparse.h"
#include "devparam.h"
struct asy Asy[ASY_MAX];
struct sgttyb mysavetty;
unsigned Nasy = 0;
#ifdef USE_OUR_OWN_FCSTAB
#ifndef PPP
/*
* This table was lifted from ppp.c and stuck here until such time
* as we can get ppp to compile under UNIX... KA9WSB
*
* FCS lookup table as generated by fcsgen.c
*/
int16 fcstab[256] = {
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
};
#endif
#endif
/* Initialize asynch port "dev" */
int
asy_init(dev,iface,arg1,arg2,bufsize,trigchar,cts,rlsd,speed)
int dev;
struct iface *iface;
char *arg1,*arg2;
int16 bufsize;
int trigchar;
char cts;
char rlsd;
int16 speed;
{
register struct asy *ap;
struct sgttyb sgttyb;
struct fifo *fp;
if (dev > Nasy)
return(-1);
ap = &Asy[dev];
#ifdef notdef
if(!strncmp(arg2,"/dev/ptyp",8)) {
slipstart(arg2);
sleep(2);
arg2[5] = 't';
}
#endif
ap->tty = strdup(arg2);
ap->cts_flow_control = cts;
ap->rlsd_line_control = rlsd;
ap->trigchar = trigchar;
/* Set up receiver FIFO */
fp = &ap->fifo;
fp->buf = mallocw(bufsize);
fp->bufsize = bufsize;
fp->wp = fp->rp = fp->buf;
fp->cnt = 0;
tprintf("asy_init: tty name = %s\n", ap->tty);
if ((ap->IORser = open (ap->tty, O_RDWR|O_NDELAY, 0)) < 0)
{
perror ("Could not open device for reading");
return (-1);
}
++Nasy;
ap->iface = iface;
if ((ap->IOWser = open (ap->tty, O_WRONLY, 0)) < 0)
{
perror ("Could not open device for writing");
ap->IOWser = ap->IORser;
#ifdef notdef
return (-1);
#endif
}
/*
* get the stty structure and save it
*/
if (ioctl (ap->IORser, TIOCGETP, &mysavetty) < 0)
{
perror ("ioctl failed on device");
return (-1);
}
/*
* copy over the structure
*/
sgttyb = mysavetty;
sgttyb.sg_flags = RAW;
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B9600;
/*
* set up the parity and enter RAW mode
*/
sgttyb.sg_flags = (RAW | ANYP | CRMOD);
/*
* now set the modes and flags
*/
if (ioctl (ap->IORser, TIOCSETP, &sgttyb) < 0)
{
perror ("ioctl could not set parameters for IORser");
return (-1);
}
if (ioctl (ap->IOWser, TIOCSETP, &sgttyb) < 0)
{
perror ("ioctl could not set parameters for IOWser");
return (-1);
}
/*
++Nasy;
ap->iface = iface;
*/
/* Enable SIGIO interrupt */
fcntl(ap->IORser,F_SETFL, fcntl(ap->IORser,F_GETFL,0) | FASYNC);
fcntl(ap->IORser,F_SETOWN,getpid());
asy_speed(dev, speed);
return (0);
}
slipstart(buf)
char *buf;
{
FILE *fp;
int i;
if(fork())
return;
for(i=0; i < Nasy; ++i) {
close(Asy[i].IORser);
close(Asy[i].IOWser);
}
close(0);
close(1);
close(2);
fp = fopen(buf,"w+");
dup2(fileno(fp),0);
dup2(fileno(fp),1);
dup2(fileno(fp),2);
execl("/usr/local/etc/sliplogin","sliplogin",NULLCHAR);
}
int
asy_stop(iface)
struct iface *iface;
{
}
/* Set asynch line speed */
int
asy_speed(dev,speed)
int dev;
int16 speed;
{
struct sgttyb sgttyb;
if(speed == 0 || dev >= Nasy)
return(-1);
if (ioctl (Asy[dev].IORser, TIOCGETP, &sgttyb) < 0)
{
perror ("ioctl could not get parameters");
return (-1);
}
switch(speed)
{
case 0:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B0;
break;
case 50:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B50;
break;
case 75:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B75;
break;
case 110:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B110;
break;
case 134:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B134;
break;
case 150:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B150;
break;
case 200:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B200;
break;
case 300:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B300;
break;
case 600:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B600;
break;
case 1200:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B1200;
break;
case 1800:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B1800;
break;
case 2400:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B2400;
break;
case 4800:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B4800;
break;
case 9600:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = B9600;
break;
case 19200:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTA;
break;
case 38400:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTB;
break;
/*
case EXTA:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTA;
break;
case EXTB:
sgttyb.sg_ispeed = sgttyb.sg_ospeed = EXTB;
break;
*/
default:
tprintf("asy_speed: Unknown speed (%d)\n", speed);
break;
}
if (ioctl (Asy[dev].IORser, TIOCSETP, &sgttyb) < 0)
{
perror ("ioctl could not set parameters for IORser");
return (-1);
}
if (ioctl (Asy[dev].IOWser, TIOCSETP, &sgttyb) < 0)
{
perror ("ioctl could not set parameters for IOWser");
return (-1);
}
Asy[dev].speed = speed;
return(0);
}
/* Send a buffer to serial transmitter */
void
asy_output(dev,buf,cnt)
int dev;
char *buf;
unsigned short cnt;
{
int i_state;
if(dev >= Nasy)
return;
i_state = dirps();
if ( write(Asy[dev].IOWser, buf, (int)cnt) < cnt)
{
perror("asy_output");
printf("asy_output: error in writing to device %d\n", dev);
restore (i_state);
return;
}
restore (i_state);
Asy[dev].txchar += cnt;
}
/* Blocking read from asynch line
* Returns the character just read
*/
int
get_asy(dev)
int dev;
{
char i_state;
register struct fifo *fp;
char c;
fp = &Asy[dev].fifo;
i_state = dirps();
while(fp->cnt == 0){
if(pwait(fp) != 0){
restore(i_state);
return -1;
}
}
fp->cnt--;
restore(i_state);
c = *fp->rp++;
if(fp->rp >= &fp->buf[fp->bufsize])
fp->rp = fp->buf;
return uchar(c);
}
int32
asy_ioctl(iface,cmd,set,val)
struct iface *iface;
int cmd;
int set;
int32 val;
{
switch(cmd){
case PARAM_SPEED:
if(set)
asy_speed(iface->dev,(int16)val);
return Asy[iface->dev].speed;
/* This stuff should be added... KA9WSB
case PARAM_DTR:
if(set)
writebit(base+MCR,MCR_DTR,(int)val);
return (inportb(base+MCR) & MCR_DTR) ? TRUE : FALSE;
case PARAM_RTS:
if(set)
writebit(base+MCR,MCR_RTS,(int)val);
return (inportb(base+MCR) & MCR_RTS) ? TRUE : FALSE;
case PARAM_DOWN:
clrbit(base+IER,(char)IER_DAV);
clrbit(base+MCR,MCR_RTS);
clrbit(base+MCR,MCR_DTR);
return FALSE;
case PARAM_UP:
setbit(base+IER,(char)IER_DAV);
setbit(base+MCR,MCR_RTS);
setbit(base+MCR,MCR_DTR);
return TRUE;
*/
};
return -1;
}
int
doasystat(argc,argv,p)
int argc;
char *argv[];
void *p;
{
register struct asy *asyp;
for(asyp = Asy;asyp < &Asy[Nasy];asyp++)
if (asyp->tty != NULLCHAR)
tprintf("%s (%s %d baud): rxchar %lu txchar %lu\n",
asyp->iface->name,asyp->tty,asyp->speed,asyp->rxchar,asyp->txchar);
return 0;
}
/* Send a message on the specified serial line */
int
asy_send(dev,bp)
int dev;
struct mbuf *bp;
{
if(dev < 0 || dev >= Nasy)
return -1;
/* The data is sent directly rather than queued for asy_tx() */
while(bp != NULLBUF) {
asy_output(dev,bp->data,bp->cnt);
/* Now do next buffer on chain */
bp = free_mbuf(bp);
}
return 0;
}
/* Serial transmit process, common to all protocols */
void
asy_tx(dev,p1,p2)
int dev;
void *p1;
void *p2;
{
/* This function is not used. Instead, each process handles the output
* by itself. This is the way it used to be, and it works satisfactory
* on the Sun.
*/
Asy[dev].iface->txproc = NULLPROC;
}
int
stxrdy(dev)
int dev;
{
return 1;
}
void
asytimer()
{
}
/* Wait for a signal that the RLSD modem status has changed */
int
get_rlsd_asy(dev, new_rlsd)
int dev;
int new_rlsd;
{
int val;
if(ioctl(dev,TIOCMGET,&val) == -1)
return -1;
return val & TIOCM_CAR;
}
/* Generate a directory listing by opening a pipe to /bin/ls.
* If full == 1, give a full listing; else return just a list of names.
*/
FILE *
dir(path,full)
char *path;
int full;
{
FILE *fp;
char cmd[1024];
if (path == NULLCHAR || path[0] == '\0')
path = ".";
if (full)
sprintf(cmd,"/bin/ls -lLF %s", path);
else
sprintf(cmd, "/bin/ls -CLF %s", path);
if ((fp = popen(cmd,"r")) == NULLFILE) {
perror("popen");
return NULLFILE;
}
return fp;
}
int
getdir(path,full,file)
char *path;
int full;
FILE *file;
{
char buf[256];
FILE *fp;
if((fp = dir(path,full)) == NULLFILE)
return -1;
while(fread(buf,256,1,fp))
fwrite(buf,256,1,file);
fclose(fp);
return 0;
}